//
//  PhotoViewController.m
//  FNer
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 刘志强. All rights reserved.
//

#import "PhotoViewController.h"
#import "DetailHtworld.h"
#import "IconViewController.h"
#import "NewsViewController.h"
#import "PhotoCollectionViewCell.h"
#import "PhotoDetailViewController.h"
#import "RESideMenu.h"
@interface PhotoViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>


@property(nonatomic,retain)NSMutableArray *htWorldArray;//显示root页面cell内容
@property(nonatomic,retain)BaseCollectionView *RootCollectionView;
@property(nonatomic,assign)BOOL isUpLoading;//记录下拉上拉刷新

@property(nonatomic,retain)UIView *aView;
@property(nonatomic,retain)UIImageView *aImageView;
@property(nonatomic,retain)UILabel *alabel;
@property(nonatomic,retain)UITapGestureRecognizer *iconTap;

@property(nonatomic,retain)UIView *bView;
@property(nonatomic,retain)UIImageView *bImageView;
@property(nonatomic,retain)UILabel *blabel;
@property(nonatomic,retain)UITapGestureRecognizer *anewTap;

@property(nonatomic,retain)UIView *cView;
@property(nonatomic,retain)UIImageView *cImageView;
@property(nonatomic,retain)UILabel *clabel;
@property(nonatomic,retain)UITapGestureRecognizer *alistTap;

@property(nonatomic,assign)NSInteger upData;
@property(nonatomic,retain)UIImageView *titleView;

@end

@implementation PhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.htWorldArray = [NSMutableArray array];
    self.isUpLoading = NO;
    self.upData = 0;
    
    
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"第二个ICON选中@3x"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    

    
//    self.aView = [[UIView alloc] initWithFrame:CGRectMake(10 * XWIDTH, 10 * XHIGHT, (354 / 3) * XWIDTH, 30 * XHIGHT)];
//    self.aView.backgroundColor = [UIColor whiteColor];
//    self.aImageView = [[UIImageView alloc] initWithFrame:CGRectMake(25 * XWIDTH, 9 * XHIGHT, 35 * XWIDTH, 12 * XHIGHT)];
//    self.aImageView.image = [UIImage imageNamed:@"icon素材_@2x.png"];
//    self.alabel = [[UILabel alloc] initWithFrame:CGRectMake(65 * XWIDTH, 3 * XHIGHT, (354 / 3 - 75), 24 * XHIGHT)];
//    self.alabel.text = @"达人";
//    self.alabel.font = [UIFont systemFontOfSize:15 * XHIGHT];
//    self.iconTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconTapAction)];
//    
//    self.bView = [[UIView alloc] initWithFrame:CGRectMake((10 + 354 / 3 + 1) * XWIDTH, 10 * XHIGHT, (354 /3) * XWIDTH, 30 * XHIGHT)];
//    self.bView.backgroundColor = [UIColor whiteColor];
//    self.bImageView = [[UIImageView alloc] initWithFrame:CGRectMake(25 * XWIDTH, 9 * XHIGHT, 35 * XWIDTH, 12 * XHIGHT)];
//    self.bImageView.image = [UIImage imageNamed:@"new素材_.png"];
//    self.blabel = [[UILabel alloc] initWithFrame:CGRectMake(65 * XWIDTH, 3 * XHIGHT, 354 / 3 - 75, 24 * XHIGHT)];
//    self.blabel.text = @"最新";
//    self.blabel.font = [UIFont systemFontOfSize:15 * XHIGHT];
//    self.anewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(newTapAction)];
//    
//    self.cView = [[UIView alloc] initWithFrame:CGRectMake((10 + 354 / 3 * 2 + 2) * XWIDTH, 10 * XHIGHT, (353 / 3) * XWIDTH, 30 * XHIGHT)];
//    self.cView.backgroundColor = [UIColor whiteColor];
//    self.cImageView = [[UIImageView alloc] initWithFrame:CGRectMake(25 * XWIDTH, 9 * XHIGHT, 35 * XWIDTH, 12 * XHIGHT)];
//    self.cImageView.image = [UIImage imageNamed:@"list素材_.png"];
//    self.clabel = [[UILabel alloc] initWithFrame:CGRectMake(65 * XWIDTH, 3 * XHIGHT, 354 / 3 - 75, 24 * XHIGHT)];
//    self.clabel.text = @"频道";
//    self.clabel.font = [UIFont systemFontOfSize:15 * XHIGHT];
//    self.alistTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(listAction)];
    
    
    //创建title
    self.titleView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 125 * XWIDTH,40 * XHIGHT)];
    self.titleView.image = [UIImage imageNamed:@"titleView.png"];
    self.navigationItem.titleView = self.titleView;
    
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"nighting"];
    if ([string isEqualToString:@"yes"]) {
#warning 改的颜色
        self.titleView.image = [UIImage imageNamed:@"titleView2.png"];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColoring:) name:@"changeColoring" object:nil];
    
    [self setRootView];
    [self addHeaderRefresh];
    [self addFooterRefresh];

    

    
}


//点击左方返回按钮触发
-(void)leftButtonAction
{
    [self.sideMenuViewController presentLeftMenuViewController];
}


//下拉刷新
-(void)addHeaderRefresh
{

   self.RootCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       self.isUpLoading = NO;
        [self getRootData];
        
    }];
    [self.RootCollectionView.header beginRefreshing];

}
//上拉刷新
-(void)addFooterRefresh
{
  self.RootCollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
      self.isUpLoading = YES;
        
       DetailHtworld *ahtWorld = [self.htWorldArray lastObject];
        self.upData = ahtWorld.recommendId - 1;
        [self getRootData];
        [self.RootCollectionView reloadData];
   
    
    }];



}

//AFN获取数据
-(void)getRootData
{
    NSString *url_string = [NSString stringWithFormat:@"http://api.imzhitu.com/operations/ztworld_querySuperbTypeSquareListV3"];
    
    NSString *str = [NSString stringWithFormat:@"%ld",self.upData];
    
    NSDictionary *body = @{@"maxId":str,@"start":@"1",@"limit":@"20",@"trimExtras":@"false",@"commentLimit":@"0",@"likedLimit":@"10",@"completeLimit":@"10",@"trimConcernId":@"false",@"nextCursor":@"0"};

    
    [AFNTools getAFN:url_string andpara:body andBlock:^(id result) {
        if (self.isUpLoading == NO) {
            [self.htWorldArray removeAllObjects];
        }
        
        NSMutableDictionary *bigDic = result;

  //      [[DataHandle shareDataHandle] huanCun:bigDic];
        
        NSMutableArray *array = [bigDic objectForKey:@"htworld"];
        for (NSMutableDictionary *dic in array) {
            DetailHtworld *ahtWorld = [[DetailHtworld alloc] init];
            [ahtWorld setValuesForKeysWithDictionary:dic];
            [self.htWorldArray addObject:ahtWorld];
        }
  
       [self.RootCollectionView.header endRefreshing];
        [self.RootCollectionView.footer endRefreshing];
        [self.RootCollectionView reloadData];
        
    } andfailBlock:^(id fail) {
        NSLog(@"没有网络");
        [self.htWorldArray removeAllObjects];
        NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentPaht = [array lastObject];
        NSString *huancunDicFilePath = [documentPaht stringByAppendingPathComponent:@"HuancunDic.plist"];
        NSMutableDictionary *bigDic = [NSKeyedUnarchiver unarchiveObjectWithFile:huancunDicFilePath];
        
        if (bigDic != nil) {
            NSMutableArray *huancunArray = [bigDic objectForKey:@"htworld"];
            for (NSMutableDictionary *dic in huancunArray) {
                DetailHtworld *ahtWorld = [[DetailHtworld alloc] init];
                [ahtWorld setValuesForKeysWithDictionary:dic];
                [self.htWorldArray addObject:ahtWorld];
            }
        }
        
        else
        {
            NSLog(@"没有缓存");
        }
        
 //       [MJRefreshFooter headerEndRefreshing];
         [self.RootCollectionView.header endRefreshing];
        
        [self.RootCollectionView reloadData];

        
    }];
    

}

//创建主页面View
-(void)setRootView
{
    //创建UICollectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(172 * XWIDTH, 210 * XHIGHT);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 9 * XWIDTH, 9 * XHIGHT, 9 * XWIDTH);
    flowLayout.headerReferenceSize = CGSizeMake(375 * XWIDTH, 50 * XHIGHT);
    
    self.RootCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, 375 * XWIDTH, (667 * XHIGHT )) collectionViewLayout:flowLayout];
    self.RootCollectionView.delegate = self;
    self.RootCollectionView.dataSource = self;
    
    [self.view addSubview:self.RootCollectionView];
    [self.RootCollectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.RootCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.htWorldArray.count;
}

//创建自定义cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCollectionViewCell *rootCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    rootCell.backgroundColor = [UIColor whiteColor];
    
 
    rootCell.ahtWord = [self.htWorldArray objectAtIndex:indexPath.row];
    
    return rootCell;
}

//uicollectionview头部区域
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
    //icon按钮
    [reusableView addSubview:self.aView];
    [self.aView addSubview:self.aImageView];
    [self.aView addSubview:self.alabel];
    [self.aView addGestureRecognizer:self.iconTap];
    //new按钮
    [reusableView addSubview:self.bView];
    [self.bView addSubview:self.bImageView];
    [self.bView addSubview:self.blabel];
    [self.bView addGestureRecognizer:self.anewTap];
    //list按钮
    [reusableView addSubview:self.cView];
    [self.cView addSubview:self.cImageView];
    [self.cView addSubview:self.clabel];
    [self.cView addGestureRecognizer:self.alistTap];
    
    return reusableView;
}

//icon,new,list轻拍手势触发
-(void)iconTapAction
{
    NSLog(@"icon");
    IconViewController *IconVC = [[IconViewController alloc] init];
   // IconVC.hidesBottomBarWhenPushed = YES;
 //   [self.myAgent hiderootBottomBar];
    [SHAREDAPPDELE.myTB isHiddenTabBar:YES withViewController:IconVC];
    [self.navigationController pushViewController:IconVC animated:NO];
}
-(void)newTapAction
{
    NSLog(@"new");
    NewsViewController *NewVC = [[NewsViewController alloc] init];
    NewVC.hidesBottomBarWhenPushed = YES;
//    [self.myAgent hiderootBottomBar];
    [self.navigationController pushViewController:NewVC animated:NO];
}
//-(void)listAction
//{
//    NSLog(@"list");
//    ListViewController *ListVC = [[ListViewController alloc] init];
//    ListVC.hidesBottomBarWhenPushed = YES;
//    [self.myAgent hiderootBottomBar];
//    [self.navigationController pushViewController:ListVC animated:NO];
//}

//点击cell触发
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"section == %ld row == %ld",(long)indexPath.section,(long)indexPath.row);
    PhotoDetailViewController *rootDetailVC = [[PhotoDetailViewController alloc] init];
    rootDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:rootDetailVC animated:NO];
    
    rootDetailVC.ahtWorld = [self.htWorldArray objectAtIndex:indexPath.row];
    
  //  [self.myAgent hiderootBottomBar];
    
}

-(void)changeColoring:(NSNotification *)notification
{
#warning 改的颜色
    self.titleView.image = [UIImage imageNamed:@"titleView2.png"];
    
    NSString *string = [notification object];
    if ([string isEqualToString:@"no"]) {
        
#warning 原有颜色
        self.titleView.image = [UIImage imageNamed:@"titleView.png"];
    }
    //保存当前状态值
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"nighting"];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
